import { defineStore } from 'pinia'
import { BaseApiApi } from '@/api'
export default defineStore('store', {
  state: () => ({
    baseCompany: [] as company[],
    baseCompanyIds: [],
    dict: {} as {
      [key:string]: dict
    },
    token: ''
  }),
  getters: {
  },
  actions: {
    resetToken(){
      return BaseApiApi.login({
        "loginName": "ysadmin",
        "password": "yskj2022"
      }).then((res: any) => {
        this.token = res
      })

    },
    getBaseData(){
      return Promise.all([
        BaseApiApi.baseCompany({
          organizationType: 0
        }),
        BaseApiApi.dictInit()
      ]).then(res => {
        res.forEach((val:any, index) => {
          switch (index) {
            case 0:
              this.baseCompany = val.map((item: company & {companyName: string}) => ({
                label: item.companyName.replace(/^新津/,''),
                id: item.id,
                color: item.color,
                latitude: item.latitude,
                longitude: item.longitude,
              }))
              this.baseCompanyIds = val.map((item: { id: any; }) => item.id)
              this.baseCompany.unshift({
                label: '全部',
                id: this.baseCompanyIds.join()
              })
              break;
            case 1:
              Object.values(val.dic).forEach((item: any) => {
                if(item){
                  item.obj = {}
                  item.children.forEach((val: { value: string | number; name: any }) => {
                    item.obj[val.value] = val.name || '-'
                  })
                } 
              })
              this.dict = val.dic
              break;
           
            default:
              break;
          }
        })
      })
    }
  },
})